package org.ymh.unix_file_system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.ymh.unix_file_system.UnixFileSystemApplication;
import org.ymh.unix_file_system.dao.FileSystemOperate;
import org.ymh.unix_file_system.dao.FoundationPropertyOperate;
import org.ymh.unix_file_system.dao.IndexOperate;
import org.ymh.unix_file_system.dao.JsonDataOperate;
import org.ymh.unix_file_system.domain.FileSystemError;
import org.ymh.unix_file_system.domain.FreeBlockLinkedList;
import org.ymh.unix_file_system.domain.GlobalVariable;

import java.util.HashMap;

/**
 * @author lenovo
 * @version 1.0
 * @description: TODO
 * @date 2023/12/18 13:58
 */
@Controller
@RequestMapping("/menu")
public class menuController {
    @Autowired
    public FileSystemOperate fileSystemOperate;
    @Autowired
    public GlobalVariable globalVariable;
    @Autowired
    public IndexOperate indexOperate;
    @Autowired
    public FoundationPropertyOperate foundationPropertyOperate;
    @Autowired
    public JsonDataOperate jsonDataOperate;
    @Autowired
    public FileSystemError error;
    private  HashMap<String,Object> rootStackMap;
    @RequestMapping("")
    public String showMenu()
    {
        if(globalVariable.foundation.initFileSystem) {
            fileSystemOperate.initByGuideBlock();
            foundationPropertyOperate.setNotInitializedFileSystem();
        }
        else
        fileSystemOperate.initByJson();

        return "menu";
    }
    @GetMapping("/reMenu")
    public String reMenu()
    {
        return "menu";
    }
    @GetMapping("/showFoundation")
    public String showFoundation(Model model)
    {
         model.addAttribute("foundation",globalVariable.foundation);
         return "foundation";
    }
    @GetMapping("/init")
    @ResponseBody
    public String init()
    {
        fileSystemOperate.initByGuideBlock();
        System.exit(0);
        return "请重新重启";
    }
    @GetMapping("/showAllocateBlock")
    public String showAllocateBlock(Model model)
    {
        HashMap<String,Object> map=new HashMap<>();
        map.put("blockBitMap",globalVariable.blockBitMap);
        model.addAllAttributes(map);
        return "allocateBlockCondition";
    }
    @GetMapping("/showAllocateInode")
    public String showAllocateInode(Model model)
    {
        HashMap<String,Object> map=new HashMap<>();
        map.put("inodeBitMap",globalVariable.inodeBitMap);
        model.addAllAttributes(map);
        return "allocateInodeCondition";
    }
    @GetMapping("/showAllocateRootStack")
    public String showAllocateRootStack(Model model)
    {
        rootStackMap=new HashMap<>();
        rootStackMap.put("root",globalVariable.rootStack.stack);
        model.addAttribute("rootStack",rootStackMap);
        return "allocateRootStackCondition";
    }
    @PostMapping("/showAllocateRootStackNext")
            public String showAllocateRootStackNext(@RequestParam("blockNumber") Integer number, Model model)
    {
        if(globalVariable.superBlock.blockCountRest>50)//避免出问题(
        {
           FreeBlockLinkedList list= jsonDataOperate.jsonFileParse(new FreeBlockLinkedList(), number + "");
           rootStackMap.put(number+"",list.stack);
        }
        model.addAttribute("rootStack",rootStackMap);
        return "allocateRootStackCondition";
    }




}
